const nodemailer = require("nodemailer");
const schedule = require('node-schedule')
const sendemail = (title, content, qqemail, time) => {
	console.log("创建连接");
	let con = nodemailer.createTransport({
		host: "smtp.qq.com", //邮箱服务的主机，如smtp.qq.com
		port: 465, //对应的端口号
		//开启安全连接
		secure: true, // secure:true for port 465, secure:false for port 587
		secureConnection: true, // use SSL

		//用户信息
		auth: {
			user: "542356155@qq.com",
			pass: "dnpsoekyylsnbbee", // QQ邮箱需要使用授权码
		},
	});
	console.log("设置收件人信息");
	//设置收件人信息
	let mailOptions = {
		from: '"Mrlih" <542356155@qq.com>"', //谁发的 格式必须 ’这里是发件人名字" <xx@qq.com>‘
		to: qqemail, //"542356155@qq.com", //发给谁
		subject: title, //主题是什么
		// text: "xin", //文本内容
		html: content //"<html><h1>world123</h1></html>", //html模板

		// //附件信息
		// attachments: [
		//   {
		//     filename: "",
		//     path: "",
		//   },
		// ],
	};

	// 使用先前创建的传输器的 sendMail 方法传递消息对象
	let p = new Promise((succ,err)=>{
		con.sendMail(mailOptions, (error, info) => {
			if (error) {
				console.log(error);
				err(error)
				return console.log(error);
			} else {
				console.log(`Message: ${info.messageId}`);
				console.log(`sent: ${info.response}`);
				succ(info.response)
			}
		});
	})
	return p
}

// 格式化日期
// const formatDate = (date) =>  {
// 	var year = date.getFullYear();
// 	var month = date.getMonth()+1;
// 	var day = date.getDate();
// 	return `${year}/${month}/${day}`
// }




// setInterval(()=>{
// 	// 发送日期
// 	let sendTime = '2021/4/9 14:01'
// 	let nowTimeStamp = new Date().getTime()
// 	let sendTimeStamp = new Date(sendTime).getTime()
// 	if(nowTimeStamp>=sendTimeStamp){
// 		sendEmail()
// 	}else{
// 		console.log('没到时间');
// 		let tt = new Date(sendTimeStamp - nowTimeStamp) / 86400000
// 		console.log(tt);
// 	}
// },1000)
module.exports = sendemail